package com.wst.system.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wst.system.config.exception.GlobalExcetion;
import com.wst.system.security.entity.SysRoleEntity;
import com.wst.system.security.entity.vo.SysRoleEntityVo;
import com.wst.system.server.entity.vo.PageSearchVo;
import com.wst.system.server.mapper.SysRoleMapper;
import com.wst.system.server.service.SysRoleService;
import com.wst.system.util.JSONUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author Xiongx
 * @version 1.0
 * @date 2021/6/11 14:49
 * @since JDK 1.8
 */
@Service("sysRoleService")
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRoleEntity> implements SysRoleService {

    @Override
    public SysRoleEntity getSysRoleEntityByUserName(String userName) {
        LambdaQueryWrapper<SysRoleEntity> sysRoleEntityLambdaQueryWrapper=new LambdaQueryWrapper<>();
        sysRoleEntityLambdaQueryWrapper.eq(SysRoleEntity::getRoleName,userName);
        return super.baseMapper.selectOne(sysRoleEntityLambdaQueryWrapper);
    }

    @Override
    public SysRoleEntity getSysRoleEntityByRoleNo(String roleNo) {
        LambdaQueryWrapper<SysRoleEntity> sysRoleEntityLambdaQueryWrapper=new LambdaQueryWrapper<>();
        sysRoleEntityLambdaQueryWrapper.eq(SysRoleEntity::getRoleno,roleNo);
        return super.baseMapper.selectOne(sysRoleEntityLambdaQueryWrapper);
    }


    @Override
    public IPage<SysRoleEntity> page(Integer pageNum, Integer pageSize, PageSearchVo vo) {
        LambdaQueryWrapper<SysRoleEntity> sysRoleEntityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(JSONUtil.isNotBlank(vo.getSearch(),"roleName")){
            sysRoleEntityLambdaQueryWrapper.like(SysRoleEntity::getRoleName,vo.getString("roleName"));
        }
        if(JSONUtil.isNotBlank(vo.getSearch(),"roleno")){
            sysRoleEntityLambdaQueryWrapper.like(SysRoleEntity::getRoleno,vo.getString("roleno"));
        }
        sysRoleEntityLambdaQueryWrapper.orderByDesc(SysRoleEntity::getCreateTime);
        IPage<SysRoleEntity> page=this.baseMapper.selectPage(new Page(pageNum,pageSize),sysRoleEntityLambdaQueryWrapper);
        return page;
    }

    @Override
    public SysRoleEntity saveOrUpdateSysRole(SysRoleEntityVo vo) throws GlobalExcetion {
        SysRoleEntity sysRoleEntity=new SysRoleEntity();
        BeanUtils.copyProperties(vo,sysRoleEntity);
        if(vo.getRoleId()!=null){
            SysRoleEntity roleEntity=super.baseMapper.selectById(vo.getRoleId());
            if(roleEntity==null){
                throw new GlobalExcetion(1,"数据不存在");
            }
            if(!roleEntity.getRoleno().equals(vo.getRoleno())){
                SysRoleEntity roleNo=this.getSysRoleEntityByRoleNo(vo.getRoleno());
                if(roleNo!=null){
                    throw new GlobalExcetion(1,vo.getRoleno()+"已存在");
                }
            }
            super.baseMapper.updateById(sysRoleEntity);
        }else{
            SysRoleEntity roleEntity=this.getSysRoleEntityByRoleNo(vo.getRoleno());
            if(roleEntity!=null){
                throw new GlobalExcetion(1,vo.getRoleno()+"已存在");
            }
            super.baseMapper.insert(sysRoleEntity);
        }
        return sysRoleEntity;
    }

    @Override
    public List<SysRoleEntity> getRoleByIds(List<Integer> roleIds) {
        LambdaQueryWrapper<SysRoleEntity> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(SysRoleEntity::getRoleId,roleIds);
        return super.baseMapper.selectList(lambdaQueryWrapper);
    }

    @Override
    public List<SysRoleEntity> getRoleByRolenos(List<String> roleNos) {
        LambdaQueryWrapper<SysRoleEntity> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(SysRoleEntity::getRoleno,roleNos);
        return super.baseMapper.selectList(lambdaQueryWrapper);
    }

    @Override
    public void deleteIds(List<String> ids) {
        this.baseMapper.deleteBatchIds(ids);
    }

    @Override
    public List<SysRoleEntity> getByRoleName(String roleName) {
        LambdaQueryWrapper<SysRoleEntity> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(roleName)){
            lambdaQueryWrapper.like(SysRoleEntity::getRoleName,roleName);
        }
        List<SysRoleEntity> list=this.baseMapper.selectList(lambdaQueryWrapper);
        if(list==null){
            list=new ArrayList<>();
        }
        return list;
    }
}
